﻿package plugin.union
{
    import cmd.*;
    import com.core.*;
    import com.sh.game.consts.*;
    import com.sh.game.net.*;
    import flash.events.*;
    import flash.utils.*;
    import manager.*;
    import pluginfw.nio.*;
    import pluginfw.nio.imsg.*;
    import view.*;
    import view.message.*;

    public class UnionGroupHandler extends Object implements IGroupHandler
    {
        public var unionCkDelay:Dictionary;
        private var timer:Timer;

        public function UnionGroupHandler()
        {
            this.unionCkDelay = new Dictionary();
            return;
        }// end function

        public function dealSimpleMessage(param1:ISimpleMessage) : void
        {
            var _loc_2:Object = null;
            var _loc_3:Object = null;
            var _loc_4:Object = null;
            switch(param1.getCmd())
            {
                case CMDUnion._51GETCKINFO:
                {
                    _loc_2 = param1.getParam();
                    if (this.unionCkDelay != null)
                    {
                        for (_loc_3 in this.unionCkDelay)
                        {
                            
                            _loc_2[_loc_3] = this.unionCkDelay[_loc_3];
                        }
                        this.unionCkDelay = null;
                    }
                    ModelProxy.change("data", "unionCk", _loc_2);
                    ModelProxy.change("union", "getCkInfo", _loc_2);
                    break;
                }
                case CMDUnion._54CKCHANGE:
                {
                    _loc_4 = param1.getParam();
                    _loc_2 = ModelProxy.getValue("data", "unionCk");
                    if (_loc_2 == null)
                    {
                        for (_loc_3 in _loc_2)
                        {
                            
                            this.unionCkDelay[_loc_3] = _loc_2[_loc_3];
                        }
                    }
                    else
                    {
                        for (_loc_3 in _loc_4)
                        {
                            
                            _loc_2[_loc_3] = _loc_4[_loc_3];
                        }
                    }
                    ModelProxy.change("union", "ckChange", _loc_2);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function dealBufferMessage(param1:IBufferMessage) : void
        {
            var result:Object;
            var input:Input;
            var unionInfo:Object;
            var ga:Object;
            var unionListArrayLength:int;
            var memberListArrayLength:int;
            var applyListArrayLength:int;
            var salary:Object;
            var buffInfoListLength:int;
            var unionTotem:Array;
            var moneyDis:int;
            var levelDis:int;
            var dis:int;
            var unionsInfo:Object;
            var listArrayLength:int;
            var selfState:int;
            var unionEmsState:Object;
            var servertime:int;
            var timeDis:int;
            var endTimeArrayLength:int;
            var redPacketRecordsArrayLength:int;
            var unionListi:int;
            var unionListObjectLength:int;
            var memberListi:int;
            var memberListObjectLength:int;
            var gameActive:Object;
            var applyListi:int;
            var applyListObjectLength:int;
            var i:int;
            var buffInfoListi:int;
            var j:int;
            var union:Object;
            var member:Object;
            var listi:int;
            var listObjectLength:int;
            var unionid:int;
            var gameactive:Object;
            var endTimei:int;
            var endTimeObjectLength:int;
            var redPacketRecordsi:int;
            var redPacketRecordsObjectLength:int;
            var message:* = param1;
            result = new Object();
            input = new Input(null);
            var bytes:* = message.getBody();
            bytes.position = 0;
            input.setBytes(bytes);
            switch(message.getCmd())
            {
                case CMDUnion._1ERROR:
                {
                    result.error = input.readString();
                    ModelProxy.change("movetips", "error", result.error);
                    break;
                }
                case CMDUnion._3GETUNIONLIST:
                {
                    unionListArrayLength = input.readShort();
                    result.unionList = new Array();
                    unionListi;
                    while (unionListi < unionListArrayLength)
                    {
                        
                        unionListObjectLength = input.readByte();
                        if (unionListObjectLength != 0)
                        {
                            result.unionList[unionListi] = new Object();
                            result.unionList[unionListi].unionId = input.readVarInt(false);
                            result.unionList[unionListi].money = input.readVarInt(false);
                            result.unionList[unionListi].creatTime = input.readVarInt(false);
                            result.unionList[unionListi].unionName = input.readString();
                            result.unionList[unionListi].unionMaster = input.readString();
                            result.unionList[unionListi].masterOnline = input.readVarInt(false);
                            result.unionList[unionListi].unionLevel = input.readVarInt(false);
                            result.unionList[unionListi].memberCount = input.readVarInt(false);
                            result.unionList[unionListi].relation = input.readShort();
                            result.unionList[unionListi].unfriendEndTime = input.readVarInt(false);
                        }
                        unionListi = (unionListi + 1);
                    }
                    ModelProxy.change("data", "unionsInfo", result.unionList);
                    ModelProxy.change("union", "getUnionList", result.unionList);
                    break;
                }
                case CMDUnion._5GETUNIONINFO:
                {
                    result.unionId = input.readVarInt(false);
                    result.unionName = input.readString();
                    result.unionLevel = input.readVarInt(false);
                    result.createTime = input.readVarInt(false);
                    result.unionRank = input.readVarInt(false);
                    result.unionMaster = input.readString();
                    result.unionMoney = input.readVarInt(false);
                    result.autoAgree = input.readVarInt(false);
                    result.notice = input.readString();
                    memberListArrayLength = input.readShort();
                    result.memberList = new Array();
                    memberListi;
                    while (memberListi < memberListArrayLength)
                    {
                        
                        memberListObjectLength = input.readByte();
                        if (memberListObjectLength != 0)
                        {
                            result.memberList[memberListi] = new Object();
                            result.memberList[memberListi].rid = input.readVarInt(false);
                            result.memberList[memberListi].name = input.readString();
                            result.memberList[memberListi].career = input.readVarInt(false);
                            result.memberList[memberListi].reinLevel = input.readVarInt(false);
                            result.memberList[memberListi].level = input.readVarInt(false);
                            result.memberList[memberListi].sex = input.readVarInt(false);
                            result.memberList[memberListi].outTime = input.readVarInt(false);
                            result.memberList[memberListi].stageId = input.readVarInt(false);
                            result.memberList[memberListi].title = input.readVarInt(false);
                            result.memberList[memberListi].online = input.readVarInt(false);
                            result.memberList[memberListi].vip = input.readVarInt(false);
                            result.memberList[memberListi].tequan = input.readVarInt(false);
                        }
                        memberListi = (memberListi + 1);
                    }
                    ModelProxy.change("data", "unionInfo", result);
                    ModelProxy.change("union", "getUnionInfo", result);
                    ModelProxy.change("unionDonate", "refer", null);
                    ModelProxy.change("plugin", "close", "unioncreate");
                    if (ModelProxy.getValue("data", "unionEmsPopWait"))
                    {
                        ModelProxy.change("data", "unionEmsPopWait", false);
                        gameActive = ConfigDictionary.data.gameactive;
                        var _loc_3:int = 0;
                        var _loc_4:* = gameActive;
                        while (_loc_4 in _loc_3)
                        {
                            
                            ga = _loc_4[_loc_3];
                            if (ga.dietype == 3)
                            {
                                DialogMgr.instance.open(AppDialog.ACTIVITY_POPUP, [ga]);
                            }
                        }
                    }
                    break;
                }
                case CMDUnion._7CREATESUCCESS:
                {
                    result.unionId = input.readVarInt(false);
                    ModelProxy.change("plugin", "close", {target:"unioncreate"});
                    break;
                }
                case CMDUnion._11JOINSUCCESS:
                {
                    result.unionId = input.readVarInt(false);
                    if (this.timer == null)
                    {
                        this.timer = new Timer(1000, 1);
                        this.timer.addEventListener(TimerEvent.TIMER, this.doDelayUpdate);
                        this.timer.start();
                    }
                    break;
                }
                case CMDUnion._14SHOWAPPLY:
                {
                    applyListArrayLength = input.readShort();
                    result.applyList = new Array();
                    applyListi;
                    while (applyListi < applyListArrayLength)
                    {
                        
                        applyListObjectLength = input.readByte();
                        if (applyListObjectLength != 0)
                        {
                            result.applyList[applyListi] = new Object();
                            result.applyList[applyListi].rid = input.readVarInt(false);
                            result.applyList[applyListi].nbValue = input.readVarInt(false);
                            result.applyList[applyListi].name = input.readString();
                            result.applyList[applyListi].career = input.readVarInt(false);
                            result.applyList[applyListi].level = input.readVarInt(false);
                            result.applyList[applyListi].reinLevel = input.readVarInt(false);
                            result.applyList[applyListi].applyTime = input.readVarInt(false);
                        }
                        applyListi = (applyListi + 1);
                    }
                    ModelProxy.change("data", "unionApplyList", result.applyList);
                    ModelProxy.change("plugin", "open", {target:"unionnews"});
                    break;
                }
                case CMDUnion._18AUTOADD:
                {
                    result.autoAgree = input.readVarInt(false);
                    unionInfo = ModelProxy.getValue("data", "unionInfo");
                    unionInfo.autoAgree = int(result.autoAgree);
                    break;
                }
                case CMDUnion._12QUIT:
                {
                    result.reason = input.readString();
                    UnionSender._2getUnionList();
                    break;
                }
                case CMDUnion._20UNIONINVITE:
                {
                    result.unionId = input.readVarInt(false);
                    result.unionName = input.readString();
                    result.inviter = input.readString();
                    Alert2.show(result.inviter + "邀请您加入" + result.unionName + "行会", "", ["确定", "取消"], function (param1:int, param2:Object) : void
            {
                if (param1 == 0)
                {
                    UnionSender._21agreeAdd(result.unionId);
                }
                else
                {
                    UnionSender._30disagreeAdd(result.unionId);
                }
                return;
            }// end function
            );
                    break;
                }
                case CMDUnion._22MEMBERLEAVE:
                {
                    result.rid = input.readVarInt(false);
                    unionInfo = ModelProxy.getValue("data", "unionInfo");
                    if (unionInfo)
                    {
                        i;
                        while (i < unionInfo.memberList.length)
                        {
                            
                            if (unionInfo.memberList[i].rid == result.rid)
                            {
                                WarningManager.show(92, [unionInfo.memberList[i].name]);
                                (unionInfo.memberList as Array).splice(i, 1);
                                break;
                            }
                            i = (i + 1);
                        }
                        ModelProxy.change("union", "updateMembers", null);
                    }
                    break;
                }
                case CMDUnion._25CHANGENOTICE:
                {
                    result.notice = input.readString();
                    result.time = input.readVarInt(false);
                    unionInfo = ModelProxy.getValue("data", "unionInfo");
                    if (unionInfo == null)
                    {
                        return;
                    }
                    unionInfo.notice = result.notice;
                    unionInfo.time = result.time;
                    ModelProxy.change("union", "notice", result);
                    break;
                }
                case CMDUnion._27GETSALARYINFO:
                {
                    result.index = input.readVarInt(false);
                    result.title = input.readVarInt(false);
                    result.flag = input.readVarInt(false);
                    ModelProxy.change("data", "unionSalary", result);
                    ModelProxy.change("union", "getSalaryInfo", null);
                    break;
                }
                case CMDUnion._29GETSALARY:
                {
                    salary = ModelProxy.getValue("data", "unionSalary");
                    salary.flag = 0;
                    ModelProxy.change("union", "getSalaryInfo", null);
                    break;
                }
                case CMDUnion._32GETTOTEM:
                {
                    buffInfoListLength = input.readShort();
                    result.buffInfoList = new Array();
                    buffInfoListi;
                    while (buffInfoListi < buffInfoListLength)
                    {
                        
                        result.buffInfoList[buffInfoListi] = input.readVarInt(false);
                        buffInfoListi = (buffInfoListi + 1);
                    }
                    ModelProxy.change("data", "unionTotem", result.buffInfoList);
                    var _loc_3:int = 0;
                    var _loc_4:* = result.buffInfoList;
                    while (_loc_4 in _loc_3)
                    {
                        
                        j = _loc_4[_loc_3];
                        if (j > 0)
                        {
                            ModelProxy.change("bufferView", "addBindBuffer", "tuteng");
                            break;
                        }
                    }
                    ModelProxy.change("union", "getTotem", null);
                    break;
                }
                case CMDUnion._34UPTOTEM:
                {
                    result.buffType = input.readVarInt(false);
                    result.level = input.readVarInt(false);
                    unionTotem = ModelProxy.getValue("data", "unionTotem");
                    unionTotem[result.buffType] = result.level;
                    ModelProxy.change("union", "upTotem", null);
                    ModelProxy.change("bufferView", "addBindBuffer", "tuteng");
                    break;
                }
                case CMDUnion._36ALLIANCE:
                {
                    result.unionId = input.readVarInt(false);
                    result.unionName = input.readString();
                    Alert2.show(result.unionName + "欲要与贵行会缔结同盟关系，从此相帮互助！", "", ["同意", "拒绝"], function (param1:int, param2:Object) : void
            {
                if (param1 == 0)
                {
                    UnionSender._38beginAlliance(result.unionId);
                }
                return;
            }// end function
            );
                    break;
                }
                case CMDUnion._42REFER:
                {
                    result.money = input.readVarInt(false);
                    result.level = input.readVarInt(false);
                    unionInfo = ModelProxy.getValue("data", "unionInfo");
                    if (unionInfo == null)
                    {
                        return;
                    }
                    moneyDis = result.money - unionInfo.unionMoney;
                    levelDis = result.level - unionInfo.unionLevel;
                    dis = result.money - unionInfo.unionMoney;
                    if (dis > 0)
                    {
                        ModelProxy.change("mainui", "logview", "获得 " + dis + " 行会资金");
                    }
                    else if (dis < 0)
                    {
                        ModelProxy.change("mainui", "logview", "失去 " + dis + " 行会资金");
                    }
                    unionInfo.unionMoney = result.money;
                    unionInfo.unionLevel = result.level;
                    ModelProxy.change("union", "refer", null);
                    ModelProxy.change("unionDonate", "refer", result);
                    break;
                }
                case CMDUnion._45ENDALLIANCE:
                {
                    result.toUnionid = input.readVarInt(false);
                    unionsInfo = ModelProxy.getValue("data", "unionsInfo");
                    var _loc_3:int = 0;
                    var _loc_4:* = unionsInfo;
                    while (_loc_4 in _loc_3)
                    {
                        
                        union = _loc_4[_loc_3];
                        if (union.unionId == result.toUnionid)
                        {
                            union.relation = 0;
                            break;
                        }
                    }
                    ModelProxy.change("union", "getUnionList", unionsInfo);
                    break;
                }
                case CMDUnion._46CHANGEPOSITION:
                {
                    result.rid = input.readVarInt(false);
                    result.title = input.readVarInt(false);
                    unionInfo = ModelProxy.getValue("data", "unionInfo");
                    var _loc_3:int = 0;
                    var _loc_4:* = unionInfo.memberList;
                    while (_loc_4 in _loc_3)
                    {
                        
                        member = _loc_4[_loc_3];
                        if (member.rid == result.rid)
                        {
                            member.title = result.title;
                            break;
                        }
                    }
                    ModelProxy.change("union", "changePosition", unionInfo);
                    break;
                }
                case CMDUnion._48ARMINFO:
                {
                    listArrayLength = input.readShort();
                    result.list = new Dictionary();
                    listi;
                    while (listi < listArrayLength)
                    {
                        
                        listObjectLength = input.readByte();
                        if (listObjectLength != 0)
                        {
                            unionid = input.readVarInt(false);
                            result.list[unionid] = input.readVarLong(false);
                        }
                        listi = (listi + 1);
                    }
                    ModelProxy.change("zone", "setUnionColor", result.list);
                    break;
                }
                case CMDUnion._56UNIONBOSSOPEN:
                {
                    result.state = input.readVarInt(false);
                    ModelProxy.change("data", "isUnionBossOpen", result.state);
                    ModelProxy.change("union", "unionBossOpen", null);
                    break;
                }
                case CMDUnion._57UNIONBOSSREWARD:
                {
                    result.bossState = input.readByte();
                    result.leftMonster = input.readVarInt(false);
                    result.num = input.readVarInt(false);
                    ModelProxy.change("mainuiInstance", "unionBossReward", result);
                    break;
                }
                case CMDUnion._59UNIONEMSSTATE:
                {
                    result.state = input.readVarInt(false);
                    result.gongGao = input.readByte();
                    result.num = input.readByte();
                    if (result.state != 1)
                    {
                        ModelProxy.change("data", "selfUnionEmsState", 0);
                    }
                    if (result.gongGao)
                    {
                    }
                    ModelProxy.change("data", "unionEmsState", result);
                    ModelProxy.change("union", "unionEmsState", result);
                    ModelProxy.change("mainui", "changeUnionEms", null);
                    selfState = ModelProxy.getValue("data", "selfUnionEmsState");
                    if (result.state == 1)
                    {
                        if (selfState != 1)
                        {
                            gameactive = ConfigDictionary.data.gameactive;
                            var _loc_3:int = 0;
                            var _loc_4:* = gameactive;
                            while (_loc_4 in _loc_3)
                            {
                                
                                ga = _loc_4[_loc_3];
                                if (ga.dietype == 3)
                                {
                                    DialogMgr.instance.open(AppDialog.ACTIVITY_POPUP, [ga]);
                                    break;
                                }
                            }
                        }
                    }
                    break;
                }
                case CMDUnion._61UNIONEMSSELFSTATE:
                {
                    result.state = input.readVarInt(false);
                    ModelProxy.change("data", "selfUnionEmsState", result.state);
                    ModelProxy.change("mainui", "changeUnionEms", null);
                    break;
                }
                case CMDUnion._63UNIONEMSFINISH:
                {
                    result.state = input.readByte();
                    unionEmsState = ModelProxy.getValue("data", "unionEmsState");
                    unionEmsState.state = 2;
                    ModelProxy.change("mainui", "changeUnionEms", null);
                    DialogMgr.instance.open(AppDialog.EMS_FAIL, {type:1, param:result.state});
                    break;
                }
                case CMDUnion._64UNIONEMSTIME:
                {
                    result.endTime = input.readVarInt(false);
                    servertime = int((ModelProxy.getValue("data", "servertime") + Number(getTimer())) / 1000);
                    timeDis = result.endTime - servertime;
                    if (timeDis > 0)
                    {
                        setTimeout(this.doTime, 2000, timeDis);
                    }
                    break;
                }
                case CMDUnion._65UNIONCOLLECTIONBOXMSG:
                {
                    endTimeArrayLength = input.readShort();
                    result.endTime = new Array();
                    endTimei;
                    while (endTimei < endTimeArrayLength)
                    {
                        
                        endTimeObjectLength = input.readByte();
                        if (endTimeObjectLength != 0)
                        {
                            result.endTime[endTimei] = new Object();
                            result.endTime[endTimei].mid = input.readVarInt(false);
                            result.endTime[endTimei].time = input.readVarInt(false);
                            result.endTime[endTimei].x = input.readVarInt(false);
                            result.endTime[endTimei].y = input.readVarInt(false);
                        }
                        endTimei = (endTimei + 1);
                    }
                    ModelProxy.change("zone", "unionCollectionBoxMsg", result.endTime);
                    break;
                }
                case CMDUnion._67ResRedPacketInfo:
                {
                    result.leftNum = input.readVarInt(false);
                    result.endTime = input.readVarInt(false);
                    result.money = input.readVarInt(false);
                    redPacketRecordsArrayLength = input.readShort();
                    result.redPacketRecords = new Array();
                    redPacketRecordsi;
                    while (redPacketRecordsi < redPacketRecordsArrayLength)
                    {
                        
                        redPacketRecordsObjectLength = input.readByte();
                        if (redPacketRecordsObjectLength != 0)
                        {
                            result.redPacketRecords[redPacketRecordsi] = new Object();
                            result.redPacketRecords[redPacketRecordsi].name = input.readString();
                            result.redPacketRecords[redPacketRecordsi].type = input.readVarInt(false);
                            result.redPacketRecords[redPacketRecordsi].money = input.readVarInt(false);
                        }
                        redPacketRecordsi = (redPacketRecordsi + 1);
                    }
                    ModelProxy.change("data", "unionredpacket", result);
                    ModelProxy.change("union", "redpacket", null);
                    break;
                }
                case CMDUnion._70ResGetRedPacket:
                {
                    result.money = input.readVarInt(false);
                    ModelProxy.change("union", "showgetmoney", result.money);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function doDelayUpdate(event:TimerEvent) : void
        {
            var _loc_2:* = UserData.getUserData("unionid");
            if (_loc_2 > 0)
            {
                UnionSender._4getUnionInfo(_loc_2);
                UnionSender._66getRedPacketInfo();
            }
            this.timer.stop();
            this.timer.removeEventListener(TimerEvent.TIMER, this.doDelayUpdate);
            this.timer = null;
            return;
        }// end function

        private function doTime(param1:int) : void
        {
            ModelProxy.change("mainui", "counter", {type:7, value:param1});
            return;
        }// end function

        private function doSend() : void
        {
            var _loc_1:* = UserData.getUserData();
            var _loc_2:* = "<font color=\'#e19f21\'>" + _loc_1.name + "</font>开启本行会活动，1MIN后犯人出发!";
            var _loc_3:Array = [_loc_1.rid, "行会管理员", 0, 0, "", 0, _loc_1.unionid, _loc_2, 1, 1, ""];
            var _loc_4:* = _loc_3.join("#!");
            Server.instance.send(110, ChatCMD.ChannelUnion, _loc_4, 0, ConnectType.SYSTEM);
            return;
        }// end function

        public function dealProxyMessage(param1:IProxyMessage) : void
        {
            return;
        }// end function

    }
}
